package com.ecoolex.cms.system.platform.util;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.alibaba.fastjson.JSON;
import com.ecoolex.cms.system.entity.VRNaviPath;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.io.FileUtil;
import com.ecoolex.framework.common.util.map.NonDirectedMap;
import com.ecoolex.framework.common.util.map.NonDirectedMap.Route;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public final class VRNaviPathUtil {

	public static void main(String[] args) {
		try {
			List<VRNaviPath> list = read(FileUtil.readString(new File("/Users/shaon/Downloads/index2.xml")));
			list.forEach(p -> {
				System.out.println(JSON.toJSONString(p));
			});
			System.out.println(shortestPath(list, "pano19466", "pano19454"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<VRNaviPath> read(String xml) throws Exception {
		Map<String, VRNaviPath> map = Maps.newHashMap();

		SAXReader reader = new SAXReader();
		Document document = reader.read(new StringReader(xml));
		document.getRootElement().elements("scene").forEach(el -> {
			Element scene = (Element) el;
			scene.elements("hotspot").stream()
					.filter(e -> Check.notNullOrTrimEmpty(((Element) e).attributeValue("linktarget"))
							&& Check.notNullOrTrimEmpty(((Element) e).attributeValue("onclick"))
							&& ((Element) e).attributeValue("onclick").startsWith("onclickspotpoint"))
					.forEach(e -> {
						Element hotspot = (Element) e;
						VRNaviPath path = new VRNaviPath();
						path.setStartPano(scene.attributeValue("name"));
						path.setEndPano(hotspot.attributeValue("linktarget"));
						path.setPorwardNaviview(String.format("%s,%s,90", hotspot.attributeValue("ath"),
								hotspot.attributeValue("atv")));
						map.put(String.format("%s-%s", path.getStartPano(), path.getEndPano()), path);

						String retreatKey = String.format("%s-%s", path.getEndPano(), path.getStartPano());
						if (map.containsKey(retreatKey)) {
							map.get(retreatKey).setRetreatNaviview(path.getPorwardNaviview());
						}
					});
		});

		return Lists.newArrayList(map.values());
	}

	public static String shortestPath(List<VRNaviPath> naviPaths, String start, String end) {
		List<VrRoute> routeList = naviPaths.stream()
				.map(path -> new VrRoute(path.getStartPano(), path.getEndPano(), path.getPorwardNaviview()))
				.collect(Collectors.toList());
		NonDirectedMap<VrRoute> map = new NonDirectedMap<>(routeList);
		return JSON.toJSONString(map.shortestPath(start, end).routePointList());
	}

	private static class VrRoute implements Route {
		// 开始地点
		private String startSpot;
		// 结束地点
		private String endSpot;
		// 方向数据
		private String naviview;

		public VrRoute(String startSpot, String endSpot, String naviview) {
			super();
			this.startSpot = startSpot;
			this.endSpot = endSpot;
			this.naviview = naviview;
		}

		public String getStartSpot() {
			return startSpot;
		}

		public String getEndSpot() {
			return endSpot;
		}

		public String getNaviview() {
			return naviview;
		}
	}
}
